//
//  WBVisitorView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/9.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit
import SnapKit

class WBVisitorView: UIView {
    
    
    var closure:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    //给外界提供一个接口，当不是首页的时候，显示别的页的内容
    func setupVisitorViewInfo(imageName:String?, title:String?) {
        
        if let name = imageName , let text = title{
        
            houseImage.image = UIImage(named: name)
            label.text = text
            
            smallIcon.hidden = true

        }else
        {
        
            //旋转
            setupAnim()
        
        }
  
        
    }
    
    func setupAnim(){
    
        //设置基本动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = M_PI * 2
        
        anim.repeatCount = MAXFLOAT

        anim.duration = 10
        
        //切换子控制器 动画不移除， 默认是移除的
        anim.removedOnCompletion = false
        
        //添加到哪个控件上面
        smallIcon.layer.addAnimation(anim, forKey: nil)
    
    }
    
    
    //visitordiscover_feed_image_smallicon
    func setupUI() {
        
        //添加控件
        addSubview(smallIcon)
        addSubview(maskImage)
        addSubview(houseImage)
        addSubview(label)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //设置约束
        smallIcon.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        houseImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        label.snp_makeConstraints { (make) in
            
            make.top.equalTo(maskImage.snp_bottom).offset(16)
            make.centerX.equalTo(self)
            make.height.equalTo(36)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            
            make.top.equalTo(label.snp_bottom).offset(16)
            make.leading.equalTo(label)
            make.size.equalTo(CGSizeMake(100, 36))
        }
        
        loginBtn.snp_makeConstraints { (make) in
            
            make.size.top.equalTo(registerBtn)
            
            make.trailing.equalTo(label)
            
        }
    
    }
    
    
    lazy var loginBtn: UIButton = {
        
        let btn = UIButton()
        
        //添加按钮点击事件
        btn.addTarget(self, action: #selector(WBVisitorView.btnClick), forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.setTitle("登录", forState: .Normal)
        
        btn.setTitleColor(AppColor, forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return btn
        
    }()

    
    lazy var registerBtn: UIButton = {
    
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(WBVisitorView.btnClick), forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.setTitle("注册", forState: .Normal)
        
        btn.setTitleColor(AppColor, forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return btn
    
    }()
    
    //实现按钮点击事件
    @objc private func btnClick(){
        //传递给控制器
        closure?()

    }

    
    
    lazy var label : UILabel = {
    
        let label = UILabel()
    
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //设置换行
        label.numberOfLines = 0
        //设置文字颜色
        label.textColor = AppColor
        //设置文字大小
        label.font = UIFont.systemFontOfSize(15)
        //固定宽度
        label.preferredMaxLayoutWidth = 240
        //设置label大小
        label.sizeToFit()
        return label
    
    }()
    
    
    //visitordiscover_feed_mask_smallicon
    lazy var maskImage:UIImageView = {
        
        let icon = UIImageView()
        
        icon.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return icon
    }()

    //visitordiscover_feed_image_house
    lazy var houseImage:UIImageView = {
        
        let icon = UIImageView()
        
        icon.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return icon
    }()

    
    lazy var smallIcon:UIImageView = {
    
        let icon = UIImageView()
    
        icon.image = UIImage(named: "visitordiscover_feed_image_smallicon")
    
        return icon
    }()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}
